function t(t, a, e) {
    return a in t ? Object.defineProperty(t, a, {
        value: e,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = e, t;
}

var a = getApp(), e = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), i = require("../../C6EFF1F4A71FB5DFA08999F3BD3CA4D1.js"), o = i.navBack, n = i.copyCanvas;

Page({
    data: {
        platform: a.globalData.platform,
        paneType: 0,
        diySize: a.globalData.diySize,
        position: 1,
        diyPageBottomHeight: a.globalData.diyPageBottomHeight,
        statusBarHeight: a.globalData.statusBarHeight,
        titleBarHeight: a.globalData.titleBarHeight,
        winWidth: a.globalData.windowWidth,
        diyBoxSelectedPath: "",
        diyBoxRecordLen: 0,
        diyBoxRecordIdx: -1,
        diyBackBoxRecordLen: 0,
        diyBackBoxRecordIdx: -1,
        diyBackBoxSelectedPath: "",
        diyBoxSelectedIdx: "0/0",
        diyBoxIdx: null,
        diyBackBoxSelectedIdx: "0/0",
        diyBackBoxIdx: null,
        isShowOutsideTip: !1,
        isCreatingDesign: !1,
        drewClassify: [],
        drewClassifyId: 0,
        drew: [],
        diyDrew: {},
        diyScale: a.globalData.diyScale,
        clothing: 1,
        decalsBoxAnimData: {},
        clothAnimData: {},
        defaultDataObj: {},
        clothingImg: {},
        faceData: {
            1: {
                name: "diyBox",
                img: "display_positive_picture"
            },
            2: {
                name: "diyBackBox",
                img: "display_opposite_picture"
            }
        },
        clothFace: 1,
        clothColor: 1,
        productImg: {
            1: {},
            2: {}
        },
        isShowTxtMask: !1,
        drewLoading: {},
        textColorList: [ "#000000", "#8000ff", "#d424ff", "#0000ff", "#00ffff", "#00e905", "#9cff00", "#ffff00", "#ff8000", "#ff0068", "#ff0000", "#ff8caf", "#ffffff", "#888888" ],
        curText: "",
        curTextType: "add",
        curTextColor: "#000",
        curIsBold: !1,
        diyImgType: "",
        decalsAnimData: {},
        diyTypeAnimData: {},
        textAnimData: {},
        isShowCopyCanvas: !1,
        inputType: "blur",
        keyBoardHeight: -a.globalData.windowWidth / 750 * 100,
        showUploadTip: !1,
        decalsTotalPrice: 0,
        isShowFaceTip: !1,
        coupon: 0,
        fromPage: ""
    },
    onLoad: function(t) {
        var e = this, i = t.clothing, o = t.from, n = 1 * t.position;
        i || (i = 1), i *= 1, o || (o = ""), n || (n = 1), this.setData({
            clothing: i,
            fromPage: o,
            position: n
        }, function() {
            e.loadDrewClassify();
            var t = a.globalData.defaultClothingData;
            t ? e.setDefaultData(t) : e.getDefaultData();
        });
    },
    onReady: function() {
        this[this.data.faceData[1].name] = this.selectComponent("#" + this.data.faceData[1].name), 
        this[this.data.faceData[2].name] = this.selectComponent("#" + this.data.faceData[2].name), 
        this.decalsBoxAnim = wx.createAnimation({
            duration: 200
        }), this.btnDecalsBoxAnim = wx.createAnimation({
            duration: 200
        }), this.changeFaceAnim = wx.createAnimation({
            duration: 200
        }), this.decalsAnim = wx.createAnimation({
            duration: 200
        }), this.textAnim = wx.createAnimation({
            duration: 200
        }), this.diyTypeAnim = wx.createAnimation({
            duration: 200
        });
    },
    chooseClothFace: function(t) {
        var a = this, e = 1 * t.currentTarget.dataset.face || 1;
        this.changeFaceAnim.left(-(e - 1) * this.data.winWidth).step();
        var i = {
            clothFace: e,
            clothAnimData: this.changeFaceAnim.export(),
            curText: "",
            curTextColor: this.data.textColorList[0],
            curIsBold: !1,
            paneType: 0
        }, o = this.data.paneType;
        this.setData(i, function() {
            a.handleAnim(o, 0);
        });
    },
    chooseClothColor: function(t) {
        this.setData({
            clothColor: 1 * t.currentTarget.dataset.color || 1
        });
    },
    getDefaultData: function() {
        var t = this;
        e.get("/v2/goods").then(function(e) {
            if (1001 === e.data.code) {
                var i = e.data.response.data;
                a.globalData.defaultClothingData = i, t.setDefaultData(i);
            }
        });
    },
    setDefaultData: function(t) {
        var a = this, e = {}, i = {}, o = void 0, n = !0, s = !1, d = void 0;
        try {
            for (var r, c = t[Symbol.iterator](); !(n = (r = c.next()).done); n = !0) {
                var l = r.value;
                if (e[l.id] = l, 1 * this.data.clothing == 1 * l.id) for (var h = 0; h < l.goods_spec.length; h++) {
                    var u = l.goods_spec[h];
                    i[u.color_id] = u, 0 === h && (o = u.color_id);
                }
            }
        } catch (t) {
            s = !0, d = t;
        } finally {
            try {
                !n && c.return && c.return();
            } finally {
                if (s) throw d;
            }
        }
        this.setData({
            defaultDataObj: e,
            clothingImg: i,
            clothColor: o
        }, function() {
            a.getMaxCouponItem();
        });
    },
    getDrew: function() {
        var t = this;
        if (1 * this.data.drewClassifyId == 0 && this.data.drewClassify.length > 0) {
            var a = this.data.drewClassify[0].drew_classify_id;
            this.setData({
                drewClassifyId: a
            }, function() {
                t.loadDrew(a);
            });
        }
    },
    loadDrewClassify: function() {
        var t = this, i = a.globalData.diyDrewClassify;
        i.length > 0 ? this.setData({
            drewClassify: i
        }, function() {
            t.getDrew();
        }) : e.get("/v2/media/mediaClassify").then(function(e) {
            if (1001 === e.data.code) {
                var i = e.data.response.data;
                a.globalData.diyDrewClassify = i;
                var o = {}, n = !0, s = !1, d = void 0;
                try {
                    for (var r, c = i[Symbol.iterator](); !(n = (r = c.next()).done); n = !0) o[r.value.drew_classify_id] = !1;
                } catch (t) {
                    s = !0, d = t;
                } finally {
                    try {
                        !n && c.return && c.return();
                    } finally {
                        if (s) throw d;
                    }
                }
                t.setData({
                    drewClassify: i,
                    drewLoading: o
                }, function() {
                    t.getDrew();
                });
            }
        });
    },
    loadDrew: function(t) {
        var i = this, o = a.globalData.diyDrewList, n = o[t];
        if (Array.isArray(n)) this.setData({
            diyDrew: o,
            drew: n
        }); else {
            var s = this.data.drewLoading;
            if (this.setData({
                drew: []
            }), s[t]) return;
            s[t] = !0, this.setData({
                drewLoading: s
            }), e.get("/v2/media/drew", {
                classify_id: t
            }).then(function(e) {
                if (s[t] = !1, i.setData({
                    drewLoading: s
                }), 1001 === e.data.code) {
                    var o = e.data.response.data, n = a.globalData.diyDrewList;
                    n[t] = o, a.globalData.diyDrewList = n, i.setData({
                        drew: o,
                        diyDrew: n
                    });
                }
            }).catch(function() {
                s[t] = !1, i.setData({
                    drewLoading: s
                });
            });
        }
    },
    tapChangeDrew: function(t) {
        var a = this, e = t.currentTarget.dataset.classifyId;
        e !== this.data.drewClassifyId && this.setData({
            drewClassifyId: e,
            drew: []
        }, function() {
            a.loadDrew(e);
        });
    },
    closePage: function() {
        wx.showModal({
            title: "是否退出编辑",
            content: "您的作品尚未完成",
            confirmText: "取消",
            cancelText: "确定",
            success: function(t) {
                t.cancel && o();
            }
        });
    },
    submit: function() {
        var t = this, a = this["" + this.data.faceData[1].name], e = this["" + this.data.faceData[2].name], i = a.getDiyData(), o = e.getDiyData();
        i.imgList.length || o.imgList.length ? this.data.isCreatingDesign || (this[this.data.faceData[this.data.clothFace].name].unselectAll(), 
        wx.showModal({
            title: "提示",
            content: "是否提交作品？",
            confirmText: "提交作品",
            cancelText: "继续完善",
            success: function(a) {
                a.confirm && t.setData({
                    isCreatingDesign: !0,
                    productImg: {
                        1: {},
                        2: {}
                    },
                    isShowCopyCanvas: !0
                }, function() {
                    t.copyCtx = wx.createCanvasContext("copyDiy", t), t.getAllPrintimg();
                });
            }
        })) : wx.showToast({
            title: "您当前还没有设计",
            icon: "none",
            duration: 2e3
        });
    },
    getAllPrintimg: function() {
        var t = this.data.productImg, a = !1, e = this.data.faceData;
        for (var i in t) {
            var o = t[i];
            if (this[e[i].name].getDiyData().imgList.length && !o.print) {
                a = !0, this.getPrintimg(e[i].name, i);
                break;
            }
        }
        a || (this.data.isShowCopyCanvas || this.setData({
            isShowCopyCanvas: !0
        }), this.getAllCoverimg());
    },
    getPrintimg: function(t, e) {
        var i = this, o = this.data.diyScale, s = this[t].getDiyData().imgList, d = 1 * this.data.position, r = a.globalData.diySize;
        n({
            diySize: r,
            position: d,
            face: e,
            ctx: this.copyCtx,
            imgList: s,
            scale: o
        }, function() {
            var t = r[1 * e == 1 ? "front" : "back"][d].width * o, a = r[1 * e == 1 ? "front" : "back"][d].height * o;
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: t,
                height: a,
                destWidth: t,
                destHeight: a,
                fileType: "png",
                canvasId: "copyDiy",
                success: function(t) {
                    var a = t.tempFilePath;
                    wx.getFileInfo({
                        filePath: a,
                        success: function(t) {
                            var o = i.data.productImg;
                            o[e].print = a, o[e].printMd5 = t.digest, o[e].printSize = t.size, i.setData({
                                productImg: o
                            }, function() {
                                i.getAllPrintimg();
                            });
                        },
                        fail: function() {
                            wx.showToast({
                                title: "生成设计失败，请重试",
                                icon: "none"
                            });
                        }
                    });
                },
                fail: function(t) {
                    this.setData({
                        isCreatingDesign: !1
                    }, function() {
                        wx.showModal({
                            title: "生成设计图失败",
                            content: JSON.stringify(t)
                        });
                    });
                }
            });
        });
    },
    getAllCoverimg: function() {
        var t = this.data.productImg, a = !1, e = this.data.faceData;
        for (var i in t) {
            var o = t[i];
            if (this[e[i].name].getDiyData().imgList.length && !o.cover) {
                a = !0, this.getCoverimg(e[i].name, i);
                break;
            }
        }
        if (!a) {
            var n = this.data.clothColor, s = this.data.clothing, d = this.data.position, r = this.data.fromPage, c = encodeURIComponent(JSON.stringify(this.getDecalsData())), l = encodeURIComponent(JSON.stringify(this.data.productImg));
            wx.navigateTo({
                url: "/pages/diy/buy?color=" + n + "&clothing=" + s + "&productImg=" + l + "&decals=" + c + "&from=" + r + "&position=" + d
            });
        }
    },
    getDecalsData: function() {
        var t = this.diyBox.getDiyData().imgList.concat(this.diyBackBox.getDiyData().imgList), a = {}, e = [], i = [], o = 0, n = !0, s = !1, d = void 0;
        try {
            for (var r, c = t[Symbol.iterator](); !(n = (r = c.next()).done); n = !0) {
                var l = r.value.decals;
                if (l && !a[l.id]) {
                    a[l.id] = !0, e.push(l.id);
                    var h = 1 * l.price_cent;
                    o += h, i.push(h);
                }
            }
        } catch (t) {
            s = !0, d = t;
        } finally {
            try {
                !n && c.return && c.return();
            } finally {
                if (s) throw d;
            }
        }
        return {
            id: e.join(","),
            price: i,
            totalPrice: o
        };
    },
    getCoverimg: function(t, a) {
        var e = this;
        this[t].getCoverimg(function(t) {
            wx.getFileInfo({
                filePath: t,
                success: function(i) {
                    var o = e.data.productImg;
                    o[a].cover = t, o[a].coverMd5 = i.digest, o[a].coverSize = i.size, e.setData({
                        isCreatingDesign: !1,
                        productImg: o
                    }, function() {
                        e.getAllCoverimg();
                    });
                },
                fail: function() {
                    wx.showToast({
                        title: "生成设计失败，请重试",
                        icon: "none"
                    });
                }
            });
        });
    },
    openPaneType: function(t) {
        var e = this, i = 1 * t.currentTarget.dataset.type, o = (this.data.paneType, this.data.faceData[this.data.clothFace].name), n = void 0, s = this.data.paneType, d = {};
        if (1 !== i) {
            if (3 === i || 4 === i) n = this["" + o].data.imgList.length > 0 && null !== this["" + o].data.idx ? 3 : 0, 
            d.paneType = n; else if (5 === i || 7 === i) {
                n = 5, d.paneType = 5;
                var r = this[o].data.idx;
                if ("text" === this.data.diyImgType && null !== r) {
                    var c = this[o].data.imgList[r];
                    d.curTextColor = c.color, d.curIsBold = c.isBold, d.curText = c.path, d.inputType = 7 === i ? "input" : "blur";
                } else d.curText = "", d.inputType = 7 === i ? "focus" : "blur";
            } else 6 === i ? ("blur" !== this.data.inputType ? n = 5 : (n = "" !== this.data.curText ? 3 : 0, 
            "" !== this.data.curText && this[this.data.faceData[this.data.clothFace].name].filterRecordList({
                type: 1,
                hold: !0
            })), d.paneType = n) : (d.paneType = i, n = i);
            5 !== i && 7 !== i && 6 !== i && (d.curText = "", d.curIsBold = !1, d.curTextColor = this.data.textColorList[0], 
            5 === s && "" !== this.data.curText && this[this.data.faceData[this.data.clothFace].name].filterRecordList({
                type: 1
            })), this.setData(d, function() {
                e.handleAnim(s, n), 0 !== i && 0 !== d.paneType || e[o].unselectAll();
            });
        } else a.globalData.hadShowUploadTip ? (this.setData({
            showUploadTip: !1
        }), wx.showActionSheet({
            itemList: [ "拍照", "从手机相册选择" ],
            success: function(t) {
                wx.chooseImage({
                    count: 1,
                    sizeType: [ "original" ],
                    sourceType: 0 === t.tapIndex ? [ "camera" ] : [ "album" ],
                    success: function(t) {
                        wx.getImageInfo({
                            src: t.tempFilePaths[0],
                            success: function(t) {
                                e[o].addImg({
                                    type: "local",
                                    img: {
                                        width: t.width,
                                        height: t.height,
                                        path: t.path,
                                        type: "img"
                                    }
                                });
                            }
                        });
                    }
                });
            }
        })) : (a.globalData.hadShowUploadTip = !0, this.setData({
            showUploadTip: !0
        }));
    },
    chooseDrew: function(t) {
        var e = t.currentTarget.dataset.img, i = 1 * t.currentTarget.dataset.width, o = 1 * t.currentTarget.dataset.height, n = 1 * t.currentTarget.dataset.id, s = a.globalData.diyDrewList[this.data.drewClassifyId], d = null, r = !0, c = !1, l = void 0;
        try {
            for (var h, u = s[Symbol.iterator](); !(r = (h = u.next()).done); r = !0) {
                var f = h.value;
                if (f.id === n) {
                    d = f;
                    break;
                }
            }
        } catch (t) {
            c = !0, l = t;
        } finally {
            try {
                !r && u.return && u.return();
            } finally {
                if (c) throw l;
            }
        }
        this[this.data.faceData[this.data.clothFace].name].addImg({
            type: "online",
            img: {
                width: i,
                height: o,
                path: e,
                type: "img",
                decals: d
            }
        });
    },
    handleDiyType: function(t) {
        this[this.data.faceData[this.data.clothFace].name].handleType(1 * t.currentTarget.dataset.type);
    },
    getDrewIdx: function(t) {
        this.handleDrewIdx(this.data.faceData[1].name, t.detail);
    },
    getBackDrewIdx: function(t) {
        this.handleDrewIdx(this.data.faceData[2].name, t.detail);
    },
    handleDrewIdx: function(a, e) {
        var i = this, o = e.idx, n = this.data.paneType, s = -this.data.winWidth / 750 * 100, d = this.diyBox.getDiyData().imgList.length && this.diyBackBox.getDiyData().imgList.length, r = this.data.isShowFaceTip;
        if ("text" === e.type || null !== o) {
            var c, l = "online" === e.imgType ? 2 : "text" === e.type && e.active && 5 === n ? 5 : e.path ? 3 : 0, h = (c = {
                paneType: l
            }, t(c, a + "SelectedIdx", null !== o ? o + 1 + "/" + e.len : null), t(c, a + "Idx", o), 
            t(c, a + "SelectedPath", e.path || ""), t(c, "diyImgType", e.type || ""), c);
            if (5 !== l && (h.inputType = "blur", h.keyBoardHeight = s), null !== o && "text" === e.type) {
                var u = this[a].data.imgList[o];
                h.curTextColor = u.color, h.curIsBold = u.isBold;
            }
            h.isShowFaceTip = d, this.setData(h, function() {
                i.handleAnim(n, l), r != d && i.getMaxCouponItem();
            });
        } else this.setData({
            paneType: 0,
            curTextColor: this.data.textColorList[0],
            curIsBold: !1,
            inputType: "blur",
            keyBoardHeight: s,
            isShowFaceTip: d
        }, function() {
            r != d && i.getMaxCouponItem(), i.handleAnim(n, 0);
        });
    },
    handleAnim: function(t, a) {
        if (t !== a) {
            var e = -this.data.winWidth / 750, i = -this.data.diyPageBottomHeight + 6 * e, o = 134 * e, n = 300 * e, s = {}, d = 0;
            0 === a && (d = 200), 2 === t ? (this.decalsAnim.bottom(i).step({
                duration: d
            }), s.decalsAnimData = this.decalsAnim.export()) : 3 === t ? (this.diyTypeAnim.bottom(o).step({
                duration: d
            }), s.diyTypeAnimData = this.diyTypeAnim.export()) : 5 === t && (this.textAnim.bottom(n).step({
                duration: d
            }), s.textAnimData = this.textAnim.export()), 2 === a ? (this.decalsAnim.bottom(0).step(), 
            s.decalsAnimData = this.decalsAnim.export()) : 3 === a ? (this.diyTypeAnim.bottom(0).step(), 
            s.diyTypeAnimData = this.diyTypeAnim.export()) : 5 === a && (this.textAnim.bottom(0).step(), 
            s.textAnimData = this.textAnim.export()), this.setData(s);
        }
    },
    unselectAll: function() {
        var t = this;
        this[this.data.faceData[this.data.clothFace].name].unselectAll();
        var a = this.data.paneType;
        this.setData({
            paneType: 0
        }, function() {
            t.handleAnim(a, 0);
        });
    },
    getRecordData: function(a) {
        var e, i = this.data.faceData[this.data.clothFace].name, o = a.detail;
        this.setData((e = {}, t(e, i + "RecordIdx", o.idx), t(e, i + "RecordLen", o.len), 
        e));
    },
    handleRecord: function(t) {
        this[this.data.faceData[this.data.clothFace].name].filterRecordList({
            type: 1 * t.currentTarget.dataset.type
        });
    },
    updatePrice: function() {
        var t = this.getDecalsData().totalPrice;
        this.setData({
            decalsTotalPrice: t
        });
    },
    getMaxCouponItem: function() {
        var i = this;
        if (a.globalData.isLogin && "team" !== this.data.fromPage) {
            var o = this.data.clothing, n = this.data.defaultDataObj[o].price + (this.data.isShowFaceTip ? 1e3 : 0) + "";
            e.post("/v2/coupon/choiseCoupon", {
                goods: JSON.stringify(t({}, o, n))
            }).then(function(t) {
                if (1001 === t.data.code) {
                    var a = t.data.response.data || {};
                    i.setData({
                        coupon: 1 * a.discount_amount || 0
                    });
                }
            });
        }
    },
    showOutsideTip: function(t) {
        this.setData({
            isShowOutsideTip: t.detail.isShow
        });
    },
    changeTextColor: function(t) {
        var a = this, e = t.currentTarget.dataset.color;
        this.setData({
            curTextColor: e
        }, function() {
            "" !== a.data.curText && a[a.data.faceData[a.data.clothFace].name].handleText(a.data.curText, e, a.data.curIsBold);
        });
    },
    changeTextIsBold: function() {
        var t = this, a = !this.data.curIsBold;
        this.setData({
            curIsBold: a
        }, function() {
            "" !== t.data.curText && t[t.data.faceData[t.data.clothFace].name].handleText(t.data.curText, t.data.curTextColor, a);
        });
    },
    textInput: function(t) {
        var a = this, e = t.type, i = this[this.data.faceData[this.data.clothFace].name];
        if ("input" === e) this.setData({
            curText: t.detail.value,
            inputType: e
        }, function() {
            i.handleText(a.data.curText, a.data.curTextColor, a.data.curIsBold);
        }); else if ("focus" === e) setTimeout(function() {
            a.setData({
                inputType: e,
                keyBoardHeight: t.detail.height
            });
        }, 100); else if ("blur" === e) {
            var o = -this.data.winWidth / 750 * 100;
            this.setData({
                inputType: e,
                keyBoardHeight: o
            }, function() {
                5 === a.data.paneType ? i.handleText(a.data.curText, a.data.curTextColor, a.data.curIsBold) : a.setData({
                    curText: "",
                    curIsBold: !1,
                    curTextColor: a.data.textColorList[0]
                }, function() {
                    a[a.data.faceData[a.data.clothFace].name].filterRecordList({
                        type: 1
                    });
                });
            });
        }
    }
});